package com.wsoft.hr.pm.manager;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.HrPmRedisKeyConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.constant.HrPmTargetGroupConstant;
import com.wsoft.hr.pm.entity.HrPmTargetEntity;
import com.wsoft.hr.pm.mapper.HrPmTargetMapper;
import com.wsoft.hr.pm.query.target.TargetListQuery;
import com.wsoft.hr.pm.service.sys.IHrOrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2025年02月17日 15:06:35
 */
@Slf4j
@Service
public class HrPmTargetManager extends ServiceImpl<HrPmTargetMapper, HrPmTargetEntity> {

    @Resource
    private IHrOrganizationService organizationService;
    @Resource
    private RedisService redisService;
    @Resource
    private HrPmTargetGroupManager targetGroupManager;


    /**
     * 根据id获取信息
     *
     * @param id
     * @return
     */
    @GetRedisCache(type = HrPmRedisKeyConstant.TARGET_INFO,
            expire_sec = HrPmRedisKeyConstant.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public HrPmTargetEntity getById(Long id) {
        return super.getById(id);
    }


    /**
     * 更新groupId 为null
     * @param id
     */
    public void updateGroupIdToNull(List<Long> groupIdList) {
        UpdateWrapper<HrPmTargetEntity> updateWrapper = new UpdateWrapper<>();
        // 设置 group_id 为 null
        updateWrapper.set("group_id", HrPmTargetGroupConstant.HR_PM_DELETE_GROUP_DEFAULT)
                // 指定更新条件
                .in("group_id", groupIdList);
        // 调用 update 方法
        super.update(updateWrapper);
    }

    /**
     * 批量更新
     * @param list
     * @return
     */
    public boolean updateBatchById(List<HrPmTargetEntity> list){
        boolean b = super.updateBatchById(list);
        if(b){
            for (HrPmTargetEntity commentEntity : list) {
                redisService.del(HrPmRedisKeyConstant.TARGET_INFO + commentEntity.getId());
            }
        }
        return b;
    }

    /**
     * 根据id修改
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(HrPmTargetEntity entity) {
        boolean b = super.updateById(entity);
        if (b) {
            redisService.del(HrPmRedisKeyConstant.TARGET_INFO + entity.getId());
        }
        return b;
    }

    /**
     * 根据code 查询
     *
     * @param codes
     * @return
     */
    public List<HrPmTargetEntity> getByCodes(List<String> codes) {
        QueryWrapper<HrPmTargetEntity> query = new QueryWrapper<>();
        query.lambda().in(HrPmTargetEntity::getTargetCode, codes);
        return list(query);
    }

    /**
     * 根据id移除
     *
     * @param id
     * @return
     */
    public boolean removeById(Long id) {
        boolean b = super.removeById(id);
        if (b) {
            redisService.del(HrPmRedisKeyConstant.TARGET_INFO + id);
        }
        return b;
    }


    /**
     * 列表查询
     *
     * @param param
     * @return
     */
    public List<HrPmTargetEntity> list(TargetListQuery param,boolean isUser) {
        QueryWrapper<HrPmTargetEntity> query = new QueryWrapper<>();
        query.lambda()
                .eq(StrUtil.isNotBlank(param.getTargetCode()), HrPmTargetEntity::getTargetCode, param.getTargetCode())
                .like(StrUtil.isNotBlank(param.getTargetName()), HrPmTargetEntity::getTargetName, param.getTargetName())
                .eq(StrUtil.isNotBlank(param.getTargetLevel()), HrPmTargetEntity::getTargetLevel, param.getTargetLevel())
                .eq(null != param.getGroupId(), HrPmTargetEntity::getGroupId, param.getGroupId())
                .eq(null != param.getHrOrgId(), HrPmTargetEntity::getHrOrgId, param.getHrOrgId())
                .eq(null != param.getIsKey(), HrPmTargetEntity::getIsKey, param.getIsKey())
                .orderByDesc(BaseEntity::getCreateTime);
        if(isUser){
            //获取当前用户==> 个人指标
            query.lambda().eq(HrPmTargetEntity::getMemberId, LoginUserUtil.getUserInfo().getLoginUserVO().getId());
        }else{
            query.lambda()
                    .eq(null != param.getEnableFlag(), HrPmTargetEntity::getEnableFlag, param.getEnableFlag())
                    .isNull(HrPmTargetEntity::getMemberId);
        }
        return list(query);
    }

    /**
     * 根据businessCode 查询
     * @param businessCode
     * @return
     */
    public HrPmTargetEntity getByBusinessCode(String businessCode) {
        QueryWrapper<HrPmTargetEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmTargetEntity::getBusinessCode, businessCode);
        return getOne(query);
    }

}
